<?php
namespace app\index\controller;
use Hanson\Vbot\Foundation\Vbot;
use Illuminate\Cache\RedisTaggedCache;
use SebastianBergmann\GlobalState\Blacklist;
use think\Controller;
use Hanson\Vbot\Message\Text;
use Endroid\QrCode\QrCode;
use Endroid\QrCode\ErrorCorrectionLevel;
use Endroid\QrCode\LabelAlignment;
class Index extends Controller
{
    public function index()
    {
        $config=config('vbot');
        $vbot=new Vbot($config);
        $vbot->messageHandler->setHandler([MessageHandler::class,'messageHandler']);
        //加载插件
//        $vbot->messageExtension->load([
//            Blacklist::class,
//            GuessNumber::class,
//        ]);
        $vbot->observer->setQrCodeObserver([Observer::class,'setQrCodeObserver']);

        $vbot->observer->setLoginSuccessObserver([Observer::class,'setLoginSuccessObserver']);

        $vbot->observer->setReLoginSuccessObserver([Observer::class, 'setReLoginSuccessObserver']);

        $vbot->observer->setExitObserver([Observer::class, 'setExitObserver']);

        $vbot->observer->setFetchContactObserver([Observer::class, 'setFetchContactObserver']);

        $vbot->observer->setBeforeMessageObserver([Observer::class, 'setBeforeMessageObserver']);

        $vbot->observer->setNeedActivateObserver([Observer::class, 'setNeedActivateObserver']);

//        $vbot->messageHandler->setHandler(function ($message){
//            Text::send($message['form']['UserName'],'I am Vbot');
//        });
        $vbot->server->serve();
    }
    public function botStart(){
        exec("php /www/wwwroot/vbottest/public/index.php index/index/index");
    }
    public function getUrl(){
        $link=db('qrcode')->where(['status'=>1])->order('id desc')->find();
        if (!empty($link)){
            return $this->success('获取成功','',$link,'');
        }else{
            return $this->error('获取失败');
        }
    }
    public function test(){
//                db('qrcode')->where('id','>',0)->update(['status'=>0]);
//        db('qrcode')->where('id','>',0)->delete();
        $vbot=config('tuling');
        echo "<pre/>";
        print_r($vbot);exit;
        return $this->fetch();
    }
    public function getQrcode(){
        $url='https://login.weixin.qq.com/l/YaoxSNXDmg==';
        $qrCode = new QrCode($url);
        $qrCode->setSize(300);
        $qrCode->setWriterByName('png');
        $qrCode->setMargin(10);
        $qrCode->setEncoding('UTF-8');
        $qrCode->setErrorCorrectionLevel(new ErrorCorrectionLevel(ErrorCorrectionLevel::HIGH));
        $qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);
        $qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]);

        $dir = 'login_qrcode';//存放的文件夹
        $filepath = ROOT_PATH . 'public/uploads/' . $dir . '/';//文件路径
        //判断文件夹是否存在
        if (!is_dir($filepath)) {
            mkdir($filepath,'755');
        }
        $qrCode->writeFile($filepath.time().'.png');

    }
}
